package com.itheima.pinda.web.agency;

import com.itheima.pinda.DTO.angency.FleetDto;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.common.utils.PageResponse;
import com.itheima.pinda.common.utils.Result;
import com.itheima.pinda.entity.agency.PdFleet;
import com.itheima.pinda.mapstruct.TruckDTO2Entity;
import com.itheima.pinda.service.PdFleetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 车队管理
 *
 * @author llhuang10
 * @date 2021/10/3 15:27
 */
@Slf4j
@Validated
@Api(tags = "车队管理")
@RestController
@RequestMapping("sys/agency/fleet")
public class FleetController {

    @Resource
    private PdFleetService pdFleetService;

    @Resource
    private TruckDTO2Entity truckDTO2Entity;

    /**
     * 添加车队
     *
     * @param dto 车队信息
     * @return 车队信息
     */
    @PostMapping
    @ApiOperation("添加车队")
    public FleetDto saveAgencyType(@Valid @RequestBody FleetDto dto) {
        return pdFleetService.saveAgencyType(dto);
    }

    /**
     * 根据id获取车队详情
     *
     * @param id 车队id
     * @return 车队信息
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取车队详情")
    public FleetDto fineById(@NotBlank(message = "车队Id不能为空") @PathVariable(name = "id") String id) {
        PdFleet pdFleet = pdFleetService.getById(id);
        return truckDTO2Entity.conver(pdFleet);
    }

    /**
     * 获取车队分页数据
     *
     * @param page        页码
     * @param pageSize    页尺寸
     * @param name        车队名称
     * @param fleetNumber 车队编号
     * @param manager     负责人id
     * @return 车队分页数据
     */
    @GetMapping("/page")
    @ApiOperation("获取车队分页数据")
    public PageResponse<FleetDto> findByPage(@RequestParam(name = "page") Integer page,
                                             @RequestParam(name = "pageSize") Integer pageSize,
                                             @RequestParam(name = "name", required = false) String name,
                                             @RequestParam(name = "fleetNumber", required = false) String fleetNumber,
                                             @RequestParam(name = "manager", required = false) String manager) {

        return pdFleetService.findByPage(page, pageSize, name, fleetNumber, manager);
    }

    /**
     * 获取车队列表
     *
     * @param ids      车队Id列表
     * @param agencyId 机构id
     * @return 车队列表
     */
    @GetMapping
    @ApiOperation("获取车队列表")
    public List<FleetDto> findAll(@RequestParam(value = "ids", required = false) List<String> ids,
                                  @RequestParam(value = "agencyId", required = false) String agencyId) {
        return pdFleetService.findAll(ids, agencyId);
    }

    /**
     * 更新车队信息
     *
     * @param id  车队id
     * @param dto 车队信息
     * @return 车队信息
     */
    @PutMapping("/{id}")
    public FleetDto updateFleet(@NotBlank(message = "车队Id不能为空") @PathVariable(name = "id") String id,
                                @Valid @RequestBody FleetDto dto) {
        return pdFleetService.updateFleet(id, dto);
    }

    /**
     * 删除车队
     *
     * @param id 车队id
     * @return 返回信息
     */
    @PutMapping("/{id}/disable")
    public Result disable(@NotBlank(message = "车队Id不能为空") @PathVariable(name = "id") String id) {
        PdFleet pdFleet = new PdFleet();
        pdFleet.setId(Long.parseLong(id));
        pdFleet.setStatus(Constant.DATA_DISABLE_STATUS.byteValue());
        boolean res = pdFleetService.updateById(pdFleet);
        return res ? Result.ok() : Result.error("删除失败, 请联系管理员");
    }
}
